﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace SoftwareEngineering_Project
{
    public partial class Myevents : System.Web.UI.Page
    {
        private SoftwareEngineering_Project.User CurrentUser
        {
            get
            {
                return (SoftwareEngineering_Project.User)Session["CurrentUser"];
            }
            set
            {
                Session["CurrentUser"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {



            if (!Page.IsPostBack)
            { 
                //Populate the reservation dropdown menu

                Reservation myReservation = new Reservation();
                SqlDataReader myreader = myReservation.getReservation("Select * from Reservation_Type");

                ConferenceType.DataSource = myreader;
                ConferenceType.DataTextField = "Descr";
                ConferenceType.DataValueField = "TypeID";
                ConferenceType.DataBind();
                myReservation.CloseConnection();

                string sqlexactroom;

                if (ConferenceType.SelectedValue == "1")//room Only
                {
                    sqlexactroom = "Select * from Room ";
                }
                else if (ConferenceType.SelectedValue == "2")//Video 
                {
                    sqlexactroom = "Select * from Room where Video=1";
                }
                else if (ConferenceType.SelectedValue == "3")//Audio
                {
                    sqlexactroom = "Select * from Room where Audio=1";
                }
                else
                {
                    sqlexactroom = "Select * from Room where Audio=1 AND Video=1";
                }

                //Populate the reservation dropdown menu
                Room myRoom = new Room();
                SqlDataReader myreader2 = myRoom.getRooms(sqlexactroom);
                Location.DataSource = myreader2;
                Location.DataTextField = "Name";
                Location.DataValueField = "Room_ID";
                Location.DataBind();
            }
        }

        protected void MyEvents_SelectedIndexChanged(object sender, EventArgs e)
        {
            //TextBox1.Text = MyEvents.Rows[MyEvents.SelectedIndex].Cells[1].Text;
            Panel1.Visible = true;
            Reservation myres = new Reservation();
            ReservationType myType = new ReservationType();
            Room myroom = new Room();
            myres.getItem("Select * from Reservation where reservationID = '" + MyEvents.Rows[MyEvents.SelectedIndex].Cells[1].Text + "'");
            ConferenceType.SelectedItem.Text = myType.getDescription("Select Descr from Reservation_Type where TypeID = '" + myres.type_ID + "'");
            title.Text = myres.Name;
            Organizer.Text = myres.Organizer;
            Location.SelectedItem.Text = myroom.getRoomName("Select Name From Room Where Room_ID = '" + myres.Room_ID + "'");
            date.Text = myres.date.Date.ToString();
            starttime.Text = myres.STime.ToString();
            endtime.Text = myres.ETime.ToString();
            VideoYes.Checked = myres.recordVideo;
            AudioYes.Checked = myres.recordAudio;
            PresentationYes.Checked = myres.presentation;
            
        }

        protected void SqlDataSource1_Selecting(object sender, EventArgs e)
        {
            SqlDataSource1.SelectCommand = "SELECT Reservation.ReservationID AS 'ID', Reservation.Res_Date AS 'Reservation Date', Reservation.STime AS 'Start Time', Reservation.ETIME AS 'End Time', " +
                "Reservation.Name AS 'Event’s title', Reservation.Organizer, Room.Name AS 'Location', Reservation.Username FROM Reservation INNER JOIN Room ON Reservation.Room_ID = Room.Room_ID ";
            if (CurrentUser != null)
            {
                if (CurrentUser.RoleID == 12)
                {
                    SqlDataSource1.SelectCommand += "WHERE (Reservation.Username = '" + CurrentUser.Username + "') ";
                }
            }
            SqlDataSource1.DataBind();
        }

        protected void endtime_TextChanged(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }

        protected void Update_Click(object sender, EventArgs e)
        {
            Reservation myreservation = new Reservation();
            string sqlcommand = "Select * from reservation where Room_ID ='" + Convert.ToInt32(Location.SelectedValue) + "' and Res_date='" + Convert.ToDateTime(date.Text).Date.ToString("yyyy-MM-dd") + "' and STime<='" + Convert.ToDateTime(endtime.Text) + "' and Etime>='" + Convert.ToDateTime(starttime.Text) + "' ";
            SqlDataReader sqlreader = myreservation.getReservation(sqlcommand);
            if (sqlreader.HasRows)//check if the event overlap with another event
            {
               // (new Logs()).CreateLog("1", "Modified Reservation(" + MyEvents.Rows[MyEvents.SelectedIndex].Cells[1].Text + ")");
                Errormessage.Visible = true;
                Errormessage.Text = " Please choose a different date or a different conference room";
            }

            else
            {

                string updatecommand = "Update Reservation Set " +
                    "Room_ID = '" + Convert.ToInt32(Location.SelectedValue) + "' , " +
                     "Name = '" + title.Text + "' , " +
                     "organizer = '" + Organizer.Text + "' , " +
                     "Res_Date = '" + Convert.ToDateTime(date.Text).Date + "' , " +
                     "STime = '" + Convert.ToDateTime(starttime.Text) + "' , " +
                      "ETime = '" + Convert.ToDateTime(endtime.Text) + "' , " +
                      "NumofAttendees = '" + 0 + "' , " +
                      "typeID = '" + Convert.ToInt32(ConferenceType.SelectedValue) + "' , " +
                      "recordVideo = '" + VideoYes.Checked + "' , " +
                      "recordAudio = '" + AudioYes.Checked + "' , " +
                      "Presentation = '" + PresentationYes.Checked + "' , " +
                      "username = '" + CurrentUser.Username + "'  Where ReservationID = '" + MyEvents.Rows[MyEvents.SelectedIndex].Cells[1].Text + "'";
                if (myreservation.ExcuteNoresultsQuery(updatecommand) == true)
                {
                    (new Logs()).CreateLog("2", "");
                    Errormessage.Visible = true;
                    Errormessage.Text = " Reservation was successfully updated!";
                    Response.AddHeader("REFRESH", "2;URL=My events.aspx");

                }
                else
                {
                    Errormessage.Visible = true;
                    Errormessage.Text = " Database Error";
                }
            }
        }

        protected void Delete_Click(object sender, EventArgs e)
        {
            Reservation myreservation = new Reservation();
            if ( myreservation.ExcuteNoresultsQuery("Delete from Reservation Where  reservationID='"+MyEvents.Rows[MyEvents.SelectedIndex].Cells[1].Text+"'")  == true)
            {
                (new Logs()).CreateLog("3", "");
                Errormessage.Visible = true;
                Errormessage.Text = " Reservation was successfully deleted!";
                Response.AddHeader("REFRESH", "2;URL=My events.aspx");

            }
            else
            {
                Errormessage.Visible = true;
                Errormessage.Text = " Database Error";
            }
        }

        protected void Button1_Click1(object sender, EventArgs e)
        {
            Reservation myReservation = new Reservation();
            string sqlcommand = "select ReservationID as 'ID',Res_Date as'Reservation Date', stime 'Start Time',etime as 'End Time',Reservation.Name as 'Event’s title',Organizer,room.Name as'Location' from reservation,Room WHERE reservation.Room_ID = Room.Room_ID AND Username ='" + CurrentUser.Username + "'";
            MyEvents.DataSource = myReservation.getReservation(sqlcommand);
            MyEvents.DataBind();
        }

    }
}
